package com.example.xinhuazidian.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.xinhuazidian.R
import com.example.xinhuazidian.bean.Qianjiashi

class QianjiashiAdapter(
    private val mContext: Context,
    private val list: MutableList<Qianjiashi.ContentBeanX>
) :
    RecyclerView.Adapter<QianjiashiAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvWord: TextView = view.findViewById(R.id.tv_word)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_tangshisanbai_type, parent, false)
        return MyViewHolder(view)
    }

    private var mOnItemClickListener: OnRecyclerItemClickListener? = null

    fun setRecyclerItemClickListener(listener: OnRecyclerItemClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnRecyclerItemClickListener {
        fun onRecyclerItemClick(position: Int)
    }

    private var pos = 0

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val list = list[position]
        with(holder) {
            tvWord.text = list.type

            tvWord.setOnClickListener {
                mOnItemClickListener?.onRecyclerItemClick(position)

                pos = holder.absoluteAdapterPosition
                notifyDataSetChanged()
            }

            if (pos == position) {
                holder.tvWord.setTextColor(
                    ContextCompat.getColor(
                        mContext,
                        R.color.colorAccent
                    )
                )
            } else {
                holder.tvWord.setTextColor(
                    ContextCompat.getColor(
                        mContext,
                        R.color.black
                    )
                )
            }
        }
    }

    override fun getItemCount() = list.size
}
